class Solution {
public:
    bool check(int a1[], int a2[], int n)
    {
        for (int i = 0; i < n; i++)
        {
            if (a1[i] != a2[i])
                return false;
        }
        return true;
    }

    vector<int> findAnagrams(string s, string p) {
        vector<int> v;
        int n = p.size();
        int left = 0, right = 0;

        int arrP[30] = { 0 };
        for (auto e : p)
            arrP[e - 'a']++;

        int count = 0;
        int arrS[30] = { 0 };
        while (right < s.size())
        {
            arrS[s[right] - 'a']++;
            if (arrS[s[right] - 'a'] <= arrP[s[right] - 'a'])
                count++;

            if (right - left + 1 > n)
            {
                if (arrS[s[left] - 'a']-- <= arrP[s[left] - 'a'])
                    count--;

                left++;
            }

            if (count == n)
                v.push_back(left);

            right++;
        }
        return v;
    }
};